package com.gitee.zhanyao.codebase.java.file.poi.service;

import com.gitee.zhanyao.codebase.java.file.poi.word.WordTemplate;
import com.gitee.zhanyao.codebase.java.file.poi.word.WordUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class WordExportService {

    String EXPORT_TEMPLATE_PATH = "classpath:template";

    public InputStream export(Map<String, String> fieldMap, String templateFilePath) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        try {
            WordTemplate template = readTemplate(templateFilePath);
            template.replaceTag(fieldMap);
            template.write(stream);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return new ByteArrayInputStream(stream.toByteArray());
    }

    public InputStream export(Map<String, String> fieldMap, List<List<List<String>>> dynamicList, int[] dynamicTableIndexs,
            String templateFilePath) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        try {
            WordTemplate template = readTemplate(templateFilePath);
            template.replaceTag(fieldMap, dynamicList, dynamicTableIndexs);
            template.write(stream);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return new ByteArrayInputStream(stream.toByteArray());
    }

    public WordTemplate readTemplate(String templateFilePath) throws Exception {
        WordTemplate template;
        if (StringUtils.isEmpty(templateFilePath)) {
            throw new Exception("模版路径不可为空！");
        }
        File file = ResourceUtils.getFile(EXPORT_TEMPLATE_PATH + templateFilePath);
        try (InputStream inputStream = new FileInputStream(file)) {
            template = new WordTemplate(inputStream);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw e;
        }
        return template;
    }

    public InputStream export(List<Map<String, String>> params, String templateFilePath) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        try {
            List<WordTemplate> templates = new ArrayList<>();
            WordTemplate replaceTemplate = null;
            for (Map<String, String> param : params) {
                replaceTemplate = readTemplate(templateFilePath);
                replaceTemplate.replaceTag(param);
                templates.add(replaceTemplate);
            }
            WordTemplate src = templates.get(0);
            for (int i = 1; i < templates.size(); i++) {
                WordUtil.appendBody(src.getDocument(), templates.get(i).getDocument());
            }
            src.write(stream);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return new ByteArrayInputStream(stream.toByteArray());
    }
}
